import Vue from 'vue'
import axios from 'axios'

import App from './App'
import router from './router'
import aes from '../util/aes_util';

import packageInfo from '../../package.json';

import ElementUI from 'element-ui';

Vue.use(ElementUI)

// import store from './store'
if (!process.env.IS_WEB) Vue.use(require('vue-electron'))

Vue.http = Vue.prototype.$http = axios;


// Add a response interceptor
axios.interceptors.response.use((response) => {
    let decodeRes = aes.decode(response.data);
    response.data = JSON.parse(decodeRes);
    console.log(`Response Data:${decodeRes}`)
    if(response.data.code){
        if (response.data.code === 403) {
            Vue.prototype.$confirm('当前账号在其他客户端登陆，请重新登陆', '下线通知', {
                confirmButtonText: '确定',
                showCancelButton: false,
                type: 'warning',
                callback: () => {
                    // window.location.href = window.location.origin + "/"
                    router.push('/')
                }
            });
            return Promise.reject(new Error(res.message || 'Error'))
        }
        // else if(response.data.code === 503){
        //     Vue.prototype.$confirm('请更换最新版本', '系统提示', {
        //         confirmButtonText: '确定',
        //         showCancelButton: false,
        //         type: 'warning',
        //         callback: () => {
        //             window.close();
        //         }
        //     });
        //     return Promise.reject(new Error(res.message || 'Error'))
        // }
        else {
            return response
        }
    }

}, (error) => {
    return Promise.reject(error.message);
});
axios.interceptors.request.use(
    config => {
      let token = localStorage.getItem('token') || '';
      if(token){
          token = JSON.parse(token)
      }
      console.log(`Request Interceptors`);
        config.headers = {
          'version': packageInfo.version,
          'Content-Type': 'application/json',
          'X-User-System-ID': 'CLIENT_USER'
        };
      if(token){
          config.headers['Authorization'] = 'Bearer ' + token
      }
      return config
    },
    error => {
      return Promise.reject(error)
    }
);

Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  components: { App },
  router,
  // store,
  template: '<App/>'
}).$mount('#app')
